/** 
 * 1. 去有题的服务器打开题目
 * 2. 解析题目并返回答案
 * 3. 提交答案
 * 模拟点击是不行的.手动点才行.
 * 自动填不加空格行不行? 估计是不行, 因为刚才我已经找出正确答案. 
 *   !!!!加了空格也不行, 就是不能自动填
 **/


let GNS = {};

import {dump_object} from "/gang/common-tool.js"
import {solution_LZ_compression} from "/contracts/compression3.LZ_compression.js"


function numbers_str_to_array(input)
{
    return input.split(',').map(Number);
}

let map_solution={
    "Subarray with Maximum Sum":(input)=>{
        // Given the following integer array, 
        // find the contiguous subarray 
        // (containing at least one number) 
        // which has the largest sum and return that sum. 
        // 'Sum' refers to the sum of all the numbers in the subarray.
        let ns = GNS;
        // 0: 对输入进行解析:
        let array = numbers_str_to_array(input);//.split(',').map(Number);
        // ns.print(array);

        // let array = input.split(',');
        // ns.print('split:');
        // ns.print(array);
        
        // 最大值:
        let max_value = -9e9;

        // 第一重循环是起点位置
        for(let start =0;start<array.length;++start)
        {
            // 第二重循环是 数组的长度
            for(let arr_length=1;arr_length<array.length-start;++arr_length)
            {
                // 取数组:
                let subArray = array.slice(start, start+arr_length);
                // ns.print("数组:",subArray);
                let sum = subArray.reduce((accumulator, currentValue) => {
                    return accumulator + currentValue;
                }, 0);
                if(max_value<sum)
                {
                    max_value = sum;                    
                }
            }// END for
        }// END for
        return max_value;
    },
    'Total Ways to Sum':(input)=>{
        let ns = GNS;
        // let sample_0 = numbers_str_to_array(input);
        ns.print("输入:",input ,"  type:", typeof input);
        let count_solution = 0;
        let recursion_sub_number = function(remain,sub_number)
        {
            if(sub_number>remain)return;
            for(let i=0;i<=remain;++i){
                if(i*sub_number < remain){
                    recursion_sub_number(remain-i*sub_number,sub_number+1);
                }else if(i*sub_number == remain){
                    ++count_solution;
                }else if(i*sub_number > remain){
                    break;
                }
            }// END for
        }
        recursion_sub_number(input,1);

        return count_solution-1;
    },
    'Total Ways to Sum II':(input,target_sum)=>{
        let ns = GNS;
        let sample_0 = numbers_str_to_array(input);
        // How many different distinct ways can the number 75 be written as a sum of integers contained in the set:
        // [1,2,5,6,8,9,10,11,13]?
        // You may use each integer in the set zero or more times.
        // ns.print("正在写解决方案");
        var count_solution=0;
        /**
         * 这次就没有sample问题了, 不需要记录方案
         */
        let recursion_sub_number = function(remain,index,arr=[])
        {
            // let bool_outputFile= (0==index);
            // const file_path_1 = "total_some_II";
            let number_x = sample_0[index];
            for(let i=0;i<=remain;++i){
                
                if(i*number_x < remain){
                    let arr2 = arr.slice();
                    arr2.push(""+i+"X"+number_x);
                    recursion_sub_number(remain-i*number_x,index+1,arr2);
                }else if(i*number_x == remain){
                    // if(bool_outputFile)
                    // {
                    //     let arr3 = arr.slice();
                    //     arr3.push(""+i+"X"+number_x);
                    //     let str_arr = arr3.join(',');
                    //     // ns.write(file_path_1+"_"+i+".txt",str_arr," , ","a")
                    //     // ns.print("写文件:",file_path_1+"_"+i+".txt " , str_arr);
                    // }
                    // else if(0 == index)
                    // {
                    //     ns.print("index:",index)
                    // }
                    // ns.print("\t\t",arr," , ", i,'X',number_x);
                    ++count_solution;
                }else if(i*number_x > remain){
                    break;
                }
            }
        }
        recursion_sub_number(target_sum,0);
        ns.print("结果:",count_solution);
        return ""+count_solution;
    },
    'Algorithmic Stock Trader I':(input)=>{
        let ns = GNS;
        let samples = numbers_str_to_array(input);
        let max_profit=0;// 最大的收入

        // 只交易一次 算出来是157
        for(let x=0;x<samples.length;++x)
        {
            for(let y=x+1;y<samples.length;++y)
            {
                // 在x买入在y卖出
                let profit = samples[y]-samples[x];
                if(max_profit<profit )
                {
                    max_profit = profit;
                }
            }
        }

        ns.print("答案:",max_profit)
        return max_profit;
    },
    'Algorithmic Stock Trader II':(input)=>{
        let ns = GNS;
        let stock_sample = numbers_str_to_array(input);
        let dp_0=0 ;// 不持有股票的最大利润
        let dp_1=0 ;// 持有股票的最大利润
        for(let i=0;i<stock_sample.length;++i)
        {
            if(0==i)
            {
                dp_0+=0;// 无法卖出
                dp_1-=stock_sample[i];
            }
            else
            {
                dp_0 = Math.max(dp_0,dp_1+stock_sample[i]);
                dp_1 = Math.max(dp_1,dp_0-stock_sample[i]);
                // ns.print("[",dp_0,"][",dp_1,"]");
            }
        }
        return Math.max(dp_0,dp_1);
    },
    'Algorithmic Stock Trader III':(input)=>{
        let ns = GNS;
        let samples = numbers_str_to_array(input);
        let stock_sample = samples;
        let sample3 = samples.slice();
        sample3.sort();
        ns.print("sample3:",sample3);

        let times = Math.pow(3,samples.length);
        ns.print("times:",times);

        // let count_transaction=0;// 交易次数限制为2
        let max_profit=0;// 最大的收入

        let is_full_transaction = (arr)=>{
            let number = 0;
            arr.forEach((item)=>{if(-1 == item)++number;});
            return 2==number;
        }// END is_too_much_transaction

        let calculate_sample = function(arr)
        {
            // 存货
            let storage = 0;// you must sell the stock before you buy it again 持仓数不能超过1
            for(let i=0;i<arr.length;++i)
            {
                let number = arr[i];
                storage+=number;
                if(storage<0 || storage==2)
                {
                    return;// 这个样本不行.
                    break;
                }
            }
            // ns.print("可行的样本:",arr);
            let profit = 0;// 利润
            for(let i=0;i<arr.length;++i)
            {
                profit+=arr[i]*(-stock_sample[i]);
            }
            // ns.print("\t\t利润:",profit);
            // list_sample_profit.push({sample:arr,profit:profit});
            return profit;
        }// END function calculate_sample

        let recursion = (arr,zero_or_one)=>{
            if(is_full_transaction(arr))
            {
                let profit = calculate_sample(arr);
                if(max_profit<profit)
                {
                    max_profit = profit;
                }
                return;// 不进一步递归
            }

            arr.push(zero_or_one);// 0,1,-1
            // ns.print("#10 ",arr);
            if(arr.length == stock_sample.length)
            {
                // ns.print("sample:",arr);
                calculate_sample(arr);
            }
            else
            {
                recursion(arr.slice(),-1);
                recursion(arr.slice(),0);
                recursion(arr.slice(),1);
            }
        
        }// END function
        // recursion([],1);
        // recursion([],0);

        //// 只交易一次 算出来是157
        // for(let x=0;x<samples.length;++x)
        // {
        //     for(let y=x+1;y<samples.length;++y)
        //     {
        //         // 在x买入在y卖出
        //         let profit = samples[y]-samples[x];
        //         if(max_profit<profit )
        //         {
        //             max_profit = profit;
        //         }
        //     }
        // }

        for(let divide_pos = 2;divide_pos<samples.length-1;++divide_pos)
        {
            let max_profit_1 = 0;
            // 只交易一次 算出来是157
            for(let x=0;x<divide_pos;++x)
            {
                for(let y=x+1;y<divide_pos;++y)
                {
                    // 在x买入在y卖出
                    let profit = samples[y]-samples[x];
                    if(max_profit_1<profit )
                    {
                        max_profit_1 = profit;
                    }
                }
            }

            let max_profit_2 = 0;
            // 下半段:
            for(let x=divide_pos;x<samples.length;++x)
            {
                for(let y=x+1;y<samples.length;++y)
                {
                    // 在x买入在y卖出
                    let profit = samples[y]-samples[x];
                    if(max_profit_2<profit )
                    {
                        max_profit_2 = profit;
                    }
                }
            }
            let sum = max_profit_1+max_profit_2;
            if(max_profit < sum)
            {
                max_profit = sum;
            }
        }

        ns.print("答案:",max_profit)
        return max_profit;
    },
    'Algorithmic Stock Trader IV':(input)=>{
        let ns = GNS;
        ns.print("# 302 输入:",input);
        let input_data = eval('('+input+')');
        ns.print("# 304 输入:",input_data);
        ns.print("可交易次数:",input_data[0]);        
        ns.print("股票样本:",input_data[1]);

        let trade_times = input_data[0];
        let stock_sample = input_data[1];
        //组元:[第一部分是持仓最大解, 第二部分是不持仓的最大解]
        let trade_state = [];// 一个二维数组, 用于存各购买次数的状态

        // 初始化数组为购买次数+1:
        // 至于为什么+1 如果不+1, 取得到最终结果吗? 第0次是为第1次做准备, 第1次才是交易1次的结果
        for(let i=0;i<trade_times+1;++i)
        {
            trade_state.push([-9e9,-9e9]);
        }

        // 遍历股票:
        for(let stock_i = 0;stock_i<stock_sample.length;++stock_i)
        {
            let current_price = stock_sample[stock_i];

            for(let i = trade_times+1-1; i >= 0 ; --i  )
            {
                if(0==i)
                {   
                    trade_state[i][1]= Math.max(trade_state[i][1] , trade_state[i][0] + current_price);
                    trade_state[i][0]= Math.max(trade_state[i][0], -current_price);
                }
                else
                {
                    trade_state[i][0]=Math.max(trade_state[i][0] , trade_state[i][1] - current_price);// 意义:在交易了n次取最大利润的情况下,买入股票
                    trade_state[i][1]=Math.max(trade_state[i][1] , trade_state[i-1][0] + current_price);// 意义:在第N-1次持仓最优解中, 取得现在卖出能取得的最优解
                }

            }// END for
        }// END for


        return trade_state[trade_times][1];
    },
    'Sanitize Parentheses in Expression':(input)=>{
        let ns = GNS;
        let value_of_close = 0;// 到0表示关好了, 1表示有前括号, -1表示有多余的后括号
        let last_length = input.length;
        do
        {
            // 如果第一个字符是后括号 , 一定要删
            if(input[0]==')')input = input.substring(1);
            // 如果第一个字符是前括号 , 一定要删
            if(input[input.length-1]=='(')input = input.substring(0,input.length-1);        
            ns.print("去头尾后的字串:",input);
            if(last_length==input.length)break;
            last_length = input.length;
        }while(input.length);

        // 子解 {substring_begin:0,substring_end:i+1,/*子串有多少解*/solution:[2,3]}

        let last_sub_string_begin = 0;
        let arr_solutions = [];

        for(let i=0;i<input.length;++i)
        {
            let char = input[i];
            if('('==char)
            {
                ++value_of_close;
            }
            if(')'==char)
            {
                --value_of_close;
            }
            if(0==last_sub_string_begin)
            {
                if(0>value_of_close)
                {
                    ns.print("有问题:",input.substring(0,i+1),"位置前:",0,"\t位置后:",i+1);
                    // 如果后面还有后括号, 那是必删除的. ns.print("[TODO],如果后面还有")
                    //生成解决方案,生成局部最优解:
                    //ns.print("子串:",)
                    let arr_pos_of_back_bracket = [];
                    for(let i_2=0;i_2<i+1;++i_2)
                    {
                        let char2 = input[i_2];
                        if(')'==char2)
                        {
                            arr_pos_of_back_bracket.push(i_2);
                        }

                    }
                    let obj_solution = {substring_begin:0,substring_end:i+1,/*子串有多少解,删除一个就行*/solution:arr_pos_of_back_bracket}
                    arr_solutions.push(obj_solution);
                    value_of_close = 0;// 子串当作解决了, 后面的串另外算
                    last_sub_string_begin = i+1;
                }
            }else
            {
                if(0>value_of_close)
                {
                    ns.print("有问题2:",input.substring(last_sub_string_begin,i+1));
                    break;
                    //生成解决方案,生成局部最优解:
                    ns.print("[TODO] 算法未完成:生成解决方案,生成局部最优解");
                }
            }
            // ns.print("字符:",char,"\t开闭:",value_of_close);
        }
        
        let str_final = "";
        if(0!=value_of_close)
        {
            ns.print("有问题3");
            if(value_of_close>0 && last_sub_string_begin < input.length)
            {
                // 数组后删除n个数
                let sub_input = input.substring(last_sub_string_begin);
                ns.print("#329  sub_input:", sub_input);
                let number_for_delete = value_of_close;
                let arr_reverse = [];
                if(1 == number_for_delete)
                {
                    for(let i=sub_input.length-1;i>=0;--i)
                    {
                        let char3 = sub_input[i];
                        if(number_for_delete && char3=='(')
                        {
                            --number_for_delete;
                        }
                        else
                        {
                            arr_reverse.push(char3);
                        }
                    }
                    ns.print("反序数组:",arr_reverse);
                    for(let i=arr_reverse.length-1;i>=0;--i)
                    {
                        str_final+=(arr_reverse[i]);
                    }
                    ns.print("最后的一串:",str_final);
                }
                else
                {
                    // 算出绝对不能删除的前括号, 进一步算出子串, 缩小问题 // 如果一个后括号, 它的话面只有一个前括号, 那么这个前括号不能删除.
                    // 如果删除后括号, 就破坏了最少删除规则
                    let arr_dont_move_case = [];// 不动的部分 [TODO]有两个不动的情况再用
                    
                    let sub_input_2 = sub_input;
                    let sub_str_2_pos = 0;
                    for(let i=1;i<sub_input.length;++i)
                    {
                        let current_char = sub_input[i];
                        if(')'==current_char)
                        {
                            let sub_str = sub_input.substring(sub_str_2_pos,i);
                            let count_left_half = 0;
                            let end_pos=-1;
                            for(let j=0;j<sub_str.length;++j)
                            {
                                if('('==sub_str[j])
                                {
                                    ++count_left_half;
                                }
                            }// END for
                            if(1==count_left_half)
                            {
                                end_pos = i+1;
                                // 声明这一串子串不能动
                                let obj_solution = {substring_begin:sub_str_2_pos+last_sub_string_begin
                                    ,substring_end:end_pos,/*子串有多少解,删除一个就行*/solution:[]}

                                arr_solutions.push(obj_solution);
                                sub_input_2 = sub_input.substring(i+1);
                                sub_str_2_pos = i+1;
                            }
                        }
                    }// END 算出不能删除的.

                    ns.print("#488 留下来的穷举问题:",sub_input_2 );

                    // 删除任意n个前括号left-half:
                    let union_string=[];//通过字符串去重, 不然数组的数组没法自己去重
                    let arr_arr_sub_solutions=[];// 数组的数组, 包含所有的可能性所有的解决方案
                    let fun_delete_left_half = (str,index_in_str,delete_or_not/* true:delete */,arr_delete_pos/* 标记删除的下标 */)=>{
                        // 基准情形:
                        if(number_for_delete == arr_delete_pos.length)
                        {
                            let str_key = arr_delete_pos.join(',');
                            if(-1==union_string.indexOf(str_key))
                            {
                                arr_arr_sub_solutions.push(arr_delete_pos);
                                union_string.push(str_key);
                            }
                        }
                        // 基准情形2:
                        else if(index_in_str>=str.length)
                        {
                        }
                        else
                        {
                            let next_pos = index_in_str+1;// 下一个下标
                            for(let i=index_in_str;i<str.length;++i)
                            {
                                if('(' == str[i])
                                {
                                    if(delete_or_not)
                                    {
                                        arr_delete_pos.push(i);
                                    }
                                    next_pos = i+1;
                                    break;
                                }
                            }
                            fun_delete_left_half(str,next_pos,true,arr_delete_pos.slice());
                            fun_delete_left_half(str,next_pos,false,arr_delete_pos.slice());
                        }
                    };
                    fun_delete_left_half(sub_input_2,0,true,[]);
                    fun_delete_left_half(sub_input_2,0,false,[]);
                    // ns.print("#525 arr_arr_sub_solutions:",arr_arr_sub_solutions);
                    // ns.print("#534 union_string:",union_string);
                    // 声明这子串同时有多个删除位置, 需要同时删除N个位置
                    let obj_solution = {substring_begin:sub_str_2_pos+last_sub_string_begin
                        ,substring_end:sub_str_2_pos+last_sub_string_begin+sub_input_2.length
                        ,/*子串有多少解,删除一个就行*/solution:arr_arr_sub_solutions};
                    arr_solutions.push(obj_solution);
                    
                }
               
            }
        }

        let fun_is_correct_solution = (str)=>{
            let close_number = 0;
            for(let i in str)
            {
                let char = str[i];
                if('(' == char)
                {
                    ++close_number;
                }
                else if(')' == char)
                {
                    --close_number;
                    if(0>close_number)return false;
                }                
            }
            if(0 == close_number)return true;
            return false;
        }
        // 在子解决方案中去重:
        for(let i=0;i<arr_solutions.length;++i)
        {
            let solution = arr_solutions[i];
            let arr_str_result = [];
            // 对应的子串:
            let sub_str = input.substring(solution.substring_begin,solution.substring_end);
            for(let j=0;j<solution.solution.length;++j)
            {
                let pos_delete = solution.solution[j] - solution.substring_begin;
                let result_str="";
                if(pos_delete!=pos_delete)
                {
                    let arr_delete_pos = solution.solution[j];  
                    for(let k=0;k<sub_str.length;++k)
                    {
                        if(-1!=arr_delete_pos.indexOf(k))continue;
                        let char = sub_str[k];
                        result_str+=char;
                    }
                    if(result_str.length &&fun_is_correct_solution(result_str) && -1==arr_str_result.indexOf(result_str))
                    {
                        arr_str_result.push(result_str);
                    }
                }
                else
                {
                    if(pos_delete ==0)
                    {
                        // 去头:
                        result_str = sub_str.substring(1,sub_str.length);
                    }
                    else if(pos_delete==sub_str.length-1)
                    {
                        // 去尾:
                        result_str = sub_str.substring(0,sub_str.length-1);                    
                    }
                    else
                    {
                        // 截成两段再拼接:
                        result_str = sub_str.substring(0,pos_delete)+sub_str.substring(pos_delete+1);
                    }
                    if(-1==arr_str_result.indexOf(result_str))
                    {
                        arr_str_result.push(result_str);
                    }
                }
            }
            if(0==solution.solution.length)// 有些子串是完美的.
            {
                // ns.print("空的solution.solution:",solution.solution);
                arr_str_result.push(sub_str);
            }
            arr_solutions[i].arr_results = arr_str_result;
        }// END 子解决方案去重

        ns.print("子解决方案:");
        ns.print(arr_solutions);

        let result_arr=[];
        // 递归生成结果集
        let fun_cursion = (arr_solutions,index_of_solution,current_str="")=>{
            if(index_of_solution>=arr_solutions.length)
            {
                result_arr.push(current_str+str_final);
                return;// 基准情形
            }
            let solution = arr_solutions[index_of_solution];
            for(let i=0;i<solution.arr_results.length;++i)
            {
                fun_cursion(arr_solutions,index_of_solution+1,current_str+solution.arr_results[i]);
            }
        }// END fun_cursion 递归生成结果集
        fun_cursion(arr_solutions,0);
        
        return result_arr;
    },
    'Find Largest Prime Factor':(input)=>{
        let ns = GNS;
        ns.print("输入数据:",input);
        let remain1=input;
        let prime_number_list = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
            101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
            211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
            307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
            401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
            503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
            601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
            701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
            809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
            907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
        ]
        prime_number_list.forEach((prime_number)=>{
            let limit = remain1;
            do{
                if(0==remain1%prime_number)
                {
                    remain1 = remain1/prime_number;
                }
                else
                {
                    break;
                }
            }while(--limit);
        });
        // ns.print("remain1:",remain1);
        return remain1;
    },
    'Minimum Path Sum in a Triangle':(input)=>{
        let ns = GNS;
        // ns.print("输入数据:",input);
        let arr_arr = eval('('+input+')');
        ns.print("输入数据2:",arr_arr);
        let index= 0;
        let sum = 0;
        let str_path = "";
        // arr_arr.forEach((arr)=>{
        //     if(index==arr.length-1)
        //     {
        //         sum+=arr[index];
        //         str_path+=arr[index];
        //         str_path+="->";
        //     }
        //     else
        //     {
        //         let num1 = arr[index];
        //         let num2 = arr[index+1];
        //         if(num2<num1)
        //         {
        //             ++index;
        //             sum+=num2;
        //             str_path+=num2;
        //             str_path+="->";
        //         }
        //         else
        //         {
        //             sum+=num1;
        //             str_path+=num1;
        //             str_path+="->";
        //         }
        //     }
        // });
        var min_sum=9e9;
        let fun_recursion = function(level=0,index=0,sum=0)
        {
            if(level >=arr_arr.length)
            {
                if(sum<min_sum)
                {
                    min_sum = sum;
                    // ns.print(":",sum);
                }
                return;
            }
            let sum_current = sum;
            let arr = arr_arr[level];
            if(index>=arr.length)return;
            if(index==arr.length-1)
            {
                sum_current+=arr[index];
                fun_recursion(level+1,index+0,sum_current);
                fun_recursion(level+1,index+1,sum_current);
            }
            else
            {
                sum_current+=arr[index];
                fun_recursion(level+1,index+0,sum_current);
                fun_recursion(level+1,index+1,sum_current);
            }
        }
        fun_recursion();
        // ns.print("路由:",str_path);
        return min_sum;
    },
    'Array Jumping Game':(input)=>{
        let ns = GNS;
        ns.print("#511 input:",input);
        let arr = numbers_str_to_array(input);
        let final_result = false;
        let fun_recursion = function(index = 0){
            if(final_result)return;
            if(arr.length<=index)return;
            if(index == arr.length-1)
            {
                final_result = true;
                return ;
            }
            let steps = arr[index];
            for(let i=1;i<=steps;++i)
            {
                fun_recursion(index+i);
            }
        };
        fun_recursion();
        return final_result?1:0;
    },
    'Merge Overlapping Intervals':(input)=>{
        let ns = GNS;
        ns.print("#533 input:",input);
        let arr_arr = eval('('+ input +')');
        // 排序数组的数组:
        for(let i=0;i<arr_arr.length-1;++i)
        {
            let bool_has_swap =false;
            for(let j=0;j<arr_arr.length-1;++j)
            {
                // 从小到大排列
                let sub_arr_1 = arr_arr[j];
                let sub_arr_2 = arr_arr[j+1];
                if(sub_arr_2[0]< sub_arr_1[0])
                {
                    let temp = sub_arr_2.slice();
                    arr_arr[j+1] = sub_arr_1;
                    arr_arr[j] = temp;
                    bool_has_swap = true;
                }
            }
            if(!bool_has_swap)break;
        }// END for
        ns.print("#554 排序结果:",arr_arr);

        // 遍历并标记删除:
        for(let i=1;i<arr_arr.length;++i)
        {
            let arr_last = arr_arr[i-1];
            ns.print("#788 arr_last:",arr_last,"\t arr_arr:",arr_arr);
            let arr_current = arr_arr[i];
            if(arr_current[0]<= arr_last[1])
            {
                arr_current[0] = 0;
                for(let reverse_i = i-1;reverse_i>=0;--reverse_i)
                {
                    let arr_reverse = arr_arr[reverse_i];
                    if(arr_reverse[0])
                    {
                        if(arr_reverse[1]<arr_current[1])
                            arr_reverse[1] = arr_current[1];
                        else if(arr_reverse[1] > arr_current[1])
                        {
                            arr_current[1] = arr_reverse[1];
                        }
                        break;
                    }
                }
            }
        }// END for 遍历并标记删除;
        ns.print("#575 标记删除:",arr_arr);
        
        // 处理删除:
        let arr_arr_2 = [];
        for(let i=0;i<arr_arr.length;++i)
        {
            let arr_current = arr_arr[i];
            if(arr_current[0])    
            {
                // 往后遍历,找最大的结尾
                arr_arr_2.push(arr_arr[i]);
            }
        }

        return JSON.stringify(arr_arr_2);
    },   
    'Encryption I: Caesar Cipher' :(str_input)=>{
        let ns = GNS;
        ns.print("#657 input:",str_input);
        let input = eval('('+ str_input +')');
        ns.print("#615 input:",input,"  type:",typeof(input));
        let str_origin = input[0];
        ns.print("str_origin:",str_origin);
        let number = input[1];
        ns.print("number:",number);
        // 字母表:
        const alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        let fun_next_index = (index_1)=>{
            let limit = alphabet.length;
            while(0>index_1&&--limit)
            {
                index_1+=alphabet.length;
            }
            if(0>=limit)ns.print("!!!!   有死循环");
            return alphabet[index_1];
        }
        let result_string = '';
        // 第一层是遍历原始的明文:
        for(let i = 0;i<str_origin.length;++i)
        {
            let origin_char = str_origin[i];
            let index_in_alphabet = alphabet.indexOf(origin_char);
            if(-1 != index_in_alphabet)
            {
                //str_origin[i] = fun_next_index(number);
                result_string+=fun_next_index(index_in_alphabet-number);
            }
            else
            {
                result_string+=origin_char;
            }
        }// END for

        return result_string;
    },
    'Encryption II: Vigenère Cipher':(str_input)=>{
        let ns = GNS;
        ns.print("#657 input:",str_input);
        let input = eval('('+ str_input +')');
        ns.print("#659 input:",input);
        let list_origin = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

        // 加密后的结果:
        let encryption_result = '';
        // 从偏移量取字符
        let func_get_char = (row,column)=>{
            const Length = list_origin.length;
            let index = (row+column)%Length;
            return list_origin[index];
        };
        // 主循环: 明文
        for(let i=0;i<input[0].length;++i)
        {
            // 次循环: keyword
            //for(let j=0;j<input[1].length;++j)
            let j = i%input[1].length;
            {
                let row = list_origin.indexOf(input[0][i]);
                let column = list_origin.indexOf(input[1][j]);
                let char = func_get_char(row,column);
                encryption_result+=char;
            }// END for keyword
        }// END for 明文

        return encryption_result;
    },
    'Spiralize Matrix':(str_input)=>{
        let ns = GNS;
        ns.print("#688 input:",str_input);
        let input = eval('('+ str_input +')');
        // ns.print("#690 input:",input,"\tLength:",input.length);
        const max_y = input.length;
        const max_x = input[0].length
        // ####     BEGIN    生成通路:    ####
        let arr_arr_path = [];
        // 创建一个空的NxN二维数组
        let array = new Array(max_x);
        for (let i = 0; i < array.length; i++) {
            array[i] = new Array(max_y);
        }
        // 将数组的所有元素设置为0
        for (let i = 0; i < array.length; i++) {
            for (let j = 0; j < array[i].length; j++) {
                array[i][j] = 0;
            }
        }
        // ####     END    生成通路:    ####
        // 开始跑图:
        let next_pos=[0,0];
        let max_step = max_y*max_x;
        let result_arr = [];
        let direction = 0;// 0:右 1:下 2:左 3:上
        do{
            // ns.print("坐标",next_pos);
            result_arr.push(input[next_pos[1]][next_pos[0]]);
            array[next_pos[1]][next_pos[0]]=1;

            let calc_next_pos=next_pos.slice();
            let func_reset_and_turn_around = ()=>{
                calc_next_pos=next_pos.slice();//还原,换方向
                direction = (direction+1)%4;
            };
            for(let inner_i = 0;inner_i < 2;++ inner_i)
            {
                let need_break = false;
                switch(direction)
                {
                case 0: // 向右
                    calc_next_pos[0]+=1;
                    if(calc_next_pos[0]>=max_x)
                    {
                        func_reset_and_turn_around();
                    }
                    else if(1==array[calc_next_pos[1]][calc_next_pos[0]])
                    {
                        func_reset_and_turn_around();
                    }
                    else
                    {
                        next_pos = calc_next_pos;
                        need_break = true;
                    }
                    break;
                case 1: // 向下
                    calc_next_pos[1]+=1;
                    if(calc_next_pos[1]>=max_y || (1 == array[calc_next_pos[1]][calc_next_pos[0]]))
                    {
                        func_reset_and_turn_around();
                    }
                    else
                    {
                        next_pos = calc_next_pos;
                        need_break = true;
                    }
                    break;
                case 2: // 向左
                    calc_next_pos[0]-=1;
                    if(calc_next_pos[0]<0|| (1 == array[calc_next_pos[1]][calc_next_pos[0]]))
                    {
                        func_reset_and_turn_around();
                    }
                    else
                    {
                        next_pos = calc_next_pos;
                        need_break = true;
                    }
                    break;
                case 3: // 向上
                    calc_next_pos[1]-=1;
                    if(calc_next_pos[1]<0|| (1 == array[calc_next_pos[1]][calc_next_pos[0]]))
                    {
                        func_reset_and_turn_around();
                    }
                    else
                    {
                        next_pos = calc_next_pos;
                        need_break = true;
                    }
                }// END switch
                if(need_break)break;
            }// END for

        }while(--max_step);
        return result_arr;
    },
    'HammingCodes: Integer to Encoded Binary':(str_input)=>{
        let ns = GNS;
        ns.print("#688 str_input:",str_input);
        let input = eval('('+ str_input +')');
        ns.print("input:",input,"\ttype:",typeof input);
        let integerToBinary = function(num){return num.toString(2);}
        let str_binary=(integerToBinary(input));//12192179596714
        let parity_arr = [0,1,2]
        let data_length = str_binary.length+3;// +1是为了给0校验位腾位置 +3是为了给前三个校验位腾位置
        for(let i=4;i<data_length;i=i*2,++data_length){
            parity_arr.push(i);
        }
        parity_arr.reverse();
        ns.print("校验位:",parity_arr,"\t数据位数:",str_binary.length,"\t校验位位数:",parity_arr.length,"\t总位数:",str_binary.length+parity_arr.length,"\tdata_length:",data_length);

        // 填入数据:
        let result_data = new Array(data_length);
        ns.print("新的字符数组有多大:",result_data.length);
        // 正序:填入数据:
        let index_origin_string = 0;
        for(let i=0;i<result_data.length;++i){
            if(-1!=parity_arr.indexOf(i) ){
                //continue;//跳过奇偶校验位
                result_data[i] = '0';
            }
            else
            {
                result_data[i] = str_binary[index_origin_string++];
            }
        }
        ns.print("第一步填充的结果:",result_data);
        // 逆序,算出各奇偶校验位的值
        for(let i=0;i<parity_arr.length;++i){
            let pos = parity_arr[i];// 奇偶校验位的位置,同时也是奇偶校验时每一步的大小
            if(0==pos){
                let count = 0;
                for(let j=0;j<result_data.length;++j){
                    if(result_data[j] == '1'){
                        ++count;
                    }
                }
                if(count%2){
                    result_data[pos]='1';
                }
                // ns.print("#77 ",pos,"\t count:",count);
            }
            else{
                // 统计:
                let count = 0;
                // 子循环:
                let state = false;// false 进行这N个数的校验, true:跳过这N个位
                for(let j=pos;j<result_data.length;j+=pos){
                    state = !state;
                    if(state){
                        for(let index_on_data = j;index_on_data<result_data.length&&index_on_data<j+pos;++index_on_data){
                            if('1'==result_data[index_on_data]){
                                ++count;
                            }
                        }// END for
                    }
                    // ns.print("#83 ",pos);
                }
                if(count%2){
                    result_data[pos]='1';
                    ns.print("#87 ",pos,"\t count:",count);
                }
                // ns.print("#88 ",pos);
            }
        }
        //ns.print("最后的结果:",result_data);
        return result_data.join('');
    },
    'HammingCodes: Encoded Binary to Integer':(str_input)=>{
        let ns = GNS;
        str_input = str_input.replace('\'','');
        str_input = str_input.replace('\'','');
        ns.print("#1070 str_input2:",str_input);
        let is_correct = false;
        for(let i=0;i<10;++i)
        {
            let str_error_pos='';
            for(let i=0;i<str_input.length;++i)
            {
                let char_current = str_input[i];
                if(0==i)
                {
                    let count_1 = 0;
                    for(let remain_i = 0;remain_i<str_input.length;++remain_i)
                    {
                        let char_remain = str_input[remain_i];
                    }// END for
                    //if((count_1%2 && '1' == char_current) || (!(count_1%2) &&  '0'==char_current))
                    if(count_1%2)
                    {
                        ns.print("0验证位 验出错误");
                    }
                    else
                    {
                        ns.print("二进制正确(第",i,"验证位)");
                    }
                }
                else if(0==((i-1)&i))
                {
                    ns.print("验证位:",i);
                    
                    // 验证:
                    let state = 0;// 0:验证这i位  ;   1: 跳过i位
                    let count_1 = 0;
                    for(let remain_i = i;remain_i<str_input.length;remain_i+=i)
                    {
                        switch(state)
                        {
                        case 0:
                        {
                            for(let inner_loop=0;inner_loop<i;++inner_loop)
                            {
                                let char_current = str_input[remain_i+inner_loop];
                                if('1'==char_current)
                                {
                                    ++count_1;
                                }
                            }
                        }
                        break;
                        }
                        state = !state;
                    }
                    if(count_1%2)
                    {
                        ns.print("#1114 验证失败 i:",i);
                        str_error_pos = '1'+str_error_pos;
                    }
                    else
                    {
                        str_error_pos = '0' + str_error_pos;
                    }
                    
                }
                else
                {
                    // ns.print("#",i);
                }
            }// END for for(let i=0;i<str_input.length;++i)
            let err_pos = parseInt(str_error_pos,2);
            ns.print("str_error_pos:",str_error_pos,"  err_pos:",err_pos);
            let arr_str = str_input.split('');
            arr_str[err_pos] = '1'==arr_str[err_pos]? '0':'1';
            str_input =arr_str.join('');

            if(0==err_pos)
            {
                is_correct = true;
            }

            let str_arrange = '';
            for(let i=0;i<str_input.length;++i)
            {
                if(0 == ((i-1)&i))continue;
                str_arrange +=str_input[i];
            }// END 整合
            
            let number = parseInt(str_arrange,2);
            ns.print("number:",number);
            if(is_correct)
            {
                break;
            }
        }

        return "#1069 还没";
    },
    'Unique Paths in a Grid I':(str_input)=>{
        let ns = GNS;
        ns.print("#1068 str_input:",str_input);
        let arr_input = eval('('+str_input+')');
        ns.print("#1070 arr_input:",arr_input);
        let arr_arr = [];
        for(let row = 0;row  < arr_input[0];++row)
        {
            let arr_current_row = [];
            let default_value = 0;
            if(row == arr_input[0]-1)default_value=1;// 如果是最后一行, 那只有一条路
            for(let column =0;column<arr_input[1];++column)
            {
                if(column==arr_input[1]-1)// 如果是最后一列, 那只有一条路
                {
                    arr_current_row.push(1);
                }
                else
                {
                    arr_current_row.push(default_value);
                }
            }
            arr_arr.push(arr_current_row);
        }// END for
        ns.print("#1090 初始的格子:", arr_arr);
        // 倒推:
        for(let row = arr_input[0]-2;row >=0;--row)
        {
            for(let column = arr_input[1]-2;column>=0;--column)
            {
                //let current_pos_ = []
                let next_pos_1 = [row+1,column];
                let next_pos_2 = [row,column+1];
                //let currentValue = 
                arr_arr[row][column]=arr_arr[row+1][column]+arr_arr[row][column+1];
            }
        }// END for 倒推
        ns.print("#1090 倒推后的的格子:", arr_arr);
        return arr_arr[0][0];
    },
    'Unique Paths in a Grid II':(str_input,number_a_line)=>{
        let ns = GNS;
        ns.print("#854 str_input:",str_input,"  number_a_line:",number_a_line);
        // 去掉最后一个逗号:
        let char_last = str_input[str_input.length-1];
        if(','==char_last)
        {
            str_input=str_input.substring(0,str_input.length-1);
        }
        // 解析成数组:
        let arr_map = numbers_str_to_array(str_input);
        ns.print("#857 arr_map:", arr_map,"  length:",arr_map.length);
        let length_x = number_a_line;
        let length_y = arr_map.length/length_x;
        let ways = 0;
        let fun_move = (current_pos)=>{//,direction
            //ns.print("当前的位置:",current_pos[0],",",current_pos[1]);
            // 是否可以向右:
            if(current_pos[0]<length_x-1)
            {
                let pos_2 = current_pos.slice();
                ++pos_2[0];                
                // 向右, 是不是碰到1:
                if( 0 == arr_map[pos_2[1]*length_x+pos_2[0]])
                {
                    fun_move(pos_2);
                }
            }
            // 是否可以向右:
            if(current_pos[1]<length_y-1)
            {
                let pos_2 = current_pos.slice();
                ++pos_2[1];
                // 向右, 是不是碰到1:
                if( 0 == arr_map[pos_2[1]*length_x+pos_2[0]])
                {
                    fun_move(pos_2);
                }
            }
            
            {
                // 是不是到终点了.
                if(current_pos[0]==length_x-1 && current_pos[1]==length_y-1)++ways;
            }
        };
        fun_move([0,0]);

        return ways;
    },
    'Find All Valid Math Expressions':(str_input)=>{
        let ns = GNS;
        //ns.print("#854 str_input:",str_input,"  number_a_line:",number_a_line);
        // ns.print("#981 输入:",str_input);
        let obj_input = eval('('+str_input+')');
        // ns.print("#983 输入:",str_input);
        let input_number_str = obj_input[0];
        let input_number_result = obj_input[1];
        ns.print("算子:",input_number_str);
        ns.print("结果:",input_number_result);
        let result_equation = [];
        // 计算乘法运算的结果:
        let result_of_multiplication = (str_equation)=>{
            if(1== str_equation.length)return Number(str_equation);
            let result=Number(str_equation[0]);
            for(let i=2;i<str_equation.length;i+=2)
            {
                // ns.print("\t ",result," * "
                //     , str_equation[i]," ("
                //     , Number( str_equation[i]),")");
                result*=Number( str_equation[i] );
            }
            return result;
        };
        let fun_result_of_equation = (str_equation)=>{
            let obj_equation = [];// 对算子进行分解, 分解成剩法为组, 由加减分割
            let sub_equation = "";
            for(let i = 0;i<str_equation.length;++i )
            {
                let char = str_equation[i];
                // 是运算符
                // let a_symbol = char;
                switch(char)
                {
                case '+':
                case '-':
                case '*':
                    obj_equation.push(Number(sub_equation));
                    obj_equation.push(char);
                    sub_equation = "";
                    break;
                default:
                    sub_equation+=char;
                    break;
                }

            }// END for
            obj_equation.push(Number(sub_equation));
            let debug_string_1 = "分解3:"+JSON.stringify(obj_equation);
            //ns.print("分解3:",obj_equation);
            let format_str = obj_equation.join('');
            // 以乘法分组
            let fun_result_of_multiplication=(arr)=>{
                return arr.reduce((total,current)=>total*current);
            };
            let arr_equations = [];
            let sub_equation_multiplication=[obj_equation[0]];// 只有数字的数组
            for(let i=1;i<obj_equation.length;++i)
            {
                let item = obj_equation[i];                
                switch(item)
                {
                case '+':
                case '-':
                    let number_multiplication = fun_result_of_multiplication(sub_equation_multiplication);
                    arr_equations.push(number_multiplication);
                    arr_equations.push(item);
                    sub_equation_multiplication = [];
                    break;
                default:
                    sub_equation_multiplication.push(item);
                    break;
                case '*':
                    break;
                }
            }
            arr_equations.push(
                fun_result_of_multiplication(sub_equation_multiplication)
                );
            // ns.print("分解结果4:",arr_equations);
            let debug_string_2 = "分解结果4:"+JSON.stringify(arr_equations);
            
            // 计算分解的结果:
            let result_number = arr_equations[0];
            for(let i=2;i<arr_equations.length;i+=2)
            {
                switch(arr_equations[i-1])
                {
                case '+' :
                    result_number+=arr_equations[i];
                    break;
                case '-':
                    result_number-=arr_equations[i];
                    break;
                }
            }// END for
            // if(result_number == input_number_result)
            // {
            //     ns.print(str_equation);
            //     ns.print(debug_string_1,"\n",debug_string_2,"\n");   
            //     ns.print("整合后的式子:",format_str);       
            // }
            // ns.print("算术结果:",result_number," 算式: ",obj_equation.join(''));
            return [result_number,format_str];
        };
        
        let fun_recursion = (index_in_numbers,result_string)=>{
            if(index_in_numbers == input_number_str.length-1)
            {
                // 基准情形
                // 删除最后一字
                let str_result_2 = result_string+input_number_str[index_in_numbers];//result_string.substring(0,result_string.length-1);
                // ns.print(str_result_2);
                let obj_number_equation_result = fun_result_of_equation(str_result_2);
                if(input_number_result == obj_number_equation_result[0])
                {
                    // 判断字数是否匹配:
                    let sum_of_number = 0;
                    for(let i=0;i<obj_number_equation_result[1].length;++i)
                    {
                        let char = obj_number_equation_result[1][i];
                        if(char>='0'&&char<='9')
                        {
                            ++sum_of_number;
                        }
                    }
                    if(sum_of_number == obj_input[0].length)
                    {
                        result_equation.push(obj_number_equation_result[1]);
                        ns.print("可行的算式:",obj_number_equation_result[1]);
                    }
                }
            }
            else
            {
                fun_recursion(index_in_numbers+1
                    ,result_string+input_number_str[index_in_numbers]);
                fun_recursion(index_in_numbers+1
                    ,result_string+input_number_str[index_in_numbers]+'+');
                fun_recursion(index_in_numbers+1
                    ,result_string+input_number_str[index_in_numbers]+'-');
                fun_recursion(index_in_numbers+1
                    ,result_string+input_number_str[index_in_numbers]+'*');
            }
        }
        fun_recursion(0,"");

        return result_equation;
    },
    'Generate IP Addresses':(str_input)=>{
        let ns = GNS;
        ns.print("#1129 str_input:",str_input);
        
        let result_list = [];

        let fun_inner = function recursion_get_number(prefix/* 前缀 */,remain_digits)
        {
            // 基准情形1:已经有了四个点:
            let count_dot = 0;
            for(let i=0;i<prefix.length;++i){
                if(prefix[i]=='.')++count_dot;
            }
            if(4==count_dot)return;

            // 基准情形2: 第一个数字是0
            if(remain_digits[0]=='0')return;

            for(let i=1;i<=3;++i){
                let str = remain_digits.substr(0,i);
                let number = Number(str);
                if(255<number)continue;

                // 基准情形3: 正好用完了数字
                if(i==remain_digits.length)
                {
                    result_list.push(prefix+remain_digits);
                }
                else
                {
                    recursion_get_number(prefix+str+'.',remain_digits.substr(i));
                }
            }
        }

        fun_inner("","319292243");
        //ns.print(result_list);

        return JSON.stringify( result_list);
    },
    'Compression III: LZ Compression':(inputString)=>{
        let ns = GNS;
        ns.print("#1299 str_input:",inputString);
        let str_result= solution_LZ_compression(inputString,ns);
        return "#1447 还没, str_result:"+str_result;
    },
    'Compression II: LZ Decompression':(inputString)=>{
        let ns = GNS;
        ns.print("#1299 str_input:",inputString);

        let state = 0;// 0:要取长度: //1:取内容
        let state_2 = 0;// 字串与引用的切换 0:从头开始 1:是字串 2: 是引用
        let length_in_run = 0;// 0:不动 //要取的长度
        let result_str = '';
        for(let i=0;i<inputString.length;++i)
        {
            switch(state)
            {
            case 0:// 取长度        
                let length = Number(inputString[i]);
                switch(state_2)
                {
                    case 0:// 一开始是字串
                        {
                            state_2 = 1;
                            length_in_run = length;
                            break;
                        }
                    case 1: 
                        // if(0==length)
                        // {
                        //     /* 不变 */
                        // }else
                        { 
                            state_2 = 2;
                        }
                        break;
                    case 2: 
                        // if(0==length){/* 不变 */}
                        // else
                        {
                            state_2 = 1 ;
                            length_in_run = length;
                        }
                        break;
                }
                // ns.print("长度:", length,"\ti:",i,"\tstate_2:",state_2);
                if(length)state = 1;
                break;
            case 1:// 取内容
                switch(state_2)
                {
                case 1:
                    if(length_in_run)
                    {
                        result_str+=inputString[i];
                        --length_in_run;
                    }
                    if(0==length_in_run)
                    {                    
                        state = 0;
                    }
                    break;
                case 2:
                    // 这里就一个数字, 上一串的尾
                    let sub_number = Number(inputString[i]);
                    let sub_str = result_str.substring(result_str.length-sub_number);
                    // ns.print("子串",sub_str);
                    let last_number = Number(inputString[i-1]);
                    let index_of_sub_string = 0;
                    for(let i=0;i<last_number;++i)
                    {
                        // ns.print("子串下标:",index_of_sub_string);
                        result_str+=sub_str[index_of_sub_string];
                        ++index_of_sub_string;
                        if(index_of_sub_string>=sub_str.length)
                        {
                            index_of_sub_string=0;
                        }
                    }
                    state = 0;
                    break;
                }
                break;
            }
            ns.print("阶段性输出:",result_str); 
        }

        return result_str;
    },
        
};





















/**
 * 去掉<br/>
 */
function get_str_without_br(str_input)
{
    let index_br = str_input.indexOf('<br>');
    return str_input.substring(0,index_br);
}

function get_str_without_question_mark(str_input)
{
    let index_q = str_input.indexOf('?');
    return str_input.substring(0,index_q);
}
/**
 * 去掉前后中括号
 */
function unbracket(str_input)
{
    if('['==str_input[0])
    {
        str_input = str_input.substring(1);
    }
    if(']'==str_input[str_input.length-1])
    {
        str_input = str_input.substring(0,str_input.length-1);
    }
    return str_input;
}

/**
 * 每道题的题目分析不一样
 */
let map_analyze_contract = {
    'Total Ways to Sum II':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        ns.print('elm_list.length:',elm_list.length);
        elm_list.forEach((elm_p)=>{
            ns.print(elm_p.innerHTML);
        });
        // 第零个p 一定是固定的解释
        // 取第二个p 也就是下标1
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let input_data = get_str_without_br(elm_span.innerHTML);
        ns.print("# 96:",input_data);
        input_data=get_str_without_question_mark(input_data);
        ns.print("# 105:",input_data);
        //ns.print("# 106:",JSON.parse("'"+input_data+"'")," length:",input_data.length);
        input_data = unbracket(input_data.trim());
        ns.print("# 123:",input_data);
        // BEGIN 从第0个span中取出数字
        let the_number = 0;
        {
            let elm_span_0=span_list[0];
            // let str = "The cost is 123.45 dollars";
            let numbers = elm_span_0.innerHTML.match(/[0-9]+/g); // 提取整数部分
            the_number = numbers;
            // ns.print("the string:",elm_span_0.innerHTML);
            // ns.print("number:",the_number,"\ttype:",typeof the_number);
            if(the_number.length)
            {
                the_number = the_number[0];
            }
            // ns.print("the_number:",the_number);
        }
        // END 从第0个span中取出数字
        return ""+map_solution['Total Ways to Sum II'](input_data,the_number);

    },
    'Total Ways to Sum':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // ns.print('elm_list.length:',elm_list.length);
        // 取第二p
        let elm_p = elm_list[1];
        // 取最后一个span 
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[span_list.length-1];
        let the_number = elm_span.innerHTML.match(/[0-9]+/g); // 提取整数部分
        if('object'==typeof the_number)
        {
            the_number = the_number[0];
        }

        return ""+map_solution['Total Ways to Sum'](the_number);
    },
    'Algorithmic Stock Trader I':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let input_data = get_str_without_br(elm_span.innerHTML);
        // input_data=get_str_without_question_mark(input_data);
        // ns.print("# 207 input_data:",input_data);
        input_data = unbracket(input_data.trim());


        return ""+map_solution['Algorithmic Stock Trader I'](input_data);
    },
    'Algorithmic Stock Trader III':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let input_data = get_str_without_br(elm_span.innerHTML);
        // input_data=get_str_without_question_mark(input_data);
        // ns.print("# 207 input_data:",input_data);
        input_data = unbracket(input_data.trim());


        return ""+map_solution['Algorithmic Stock Trader III'](input_data);
    },
    'Algorithmic Stock Trader IV':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let input_data = get_str_without_br(elm_span.innerHTML);

        return ""+map_solution['Algorithmic Stock Trader IV'](input_data);
    },
    'Sanitize Parentheses in Expression':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let input_data = get_str_without_br(elm_span.innerHTML).trim();

        ns.print("#421 输入数据:",input_data,"\t第一个字符:",input_data[0]);
        return map_solution['Sanitize Parentheses in Expression'](input_data);
    },
    'Find Largest Prime Factor':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取第三个span 也就是下标2
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[0];

        // BEGIN 从第0个span中取出数字
        let the_number = 0;
        {
            let elm_span_0=span_list[0];
            // let str = "The cost is 123.45 dollars";
            let numbers = elm_span_0.innerHTML.match(/[0-9]+/g); // 提取整数部分
            the_number = numbers;
            // ns.print("the string:",elm_span_0.innerHTML);
            // ns.print("number:",the_number,"\ttype:",typeof the_number);
            if(the_number.length)
            {
                the_number = the_number[0];
            }
            // ns.print("the_number:",the_number);
        }
        // END 从第0个span中取出数字

        return map_solution['Find Largest Prime Factor'](the_number);
    },
    'Minimum Path Sum in a Triangle':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取第2个span到第n-1个span
        let span_list = elm_p.querySelectorAll('span');
        let span_list_2 = [];
        for(let i=1;i<span_list.length-1;++i)
        {
            let str_html = span_list[i].innerHTML;
            if(-1!=str_html.indexOf( 'Example'))break;
            str_html=str_html.replace(/&nbsp;/g,'');
            str_html=str_html.replace(/<br>/g,'');
            span_list_2.push(str_html);
        }// END for

        let str_input = span_list_2.join("\n");

        return map_solution['Minimum Path Sum in a Triangle'](str_input);

    },
    'Array Jumping Game':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取第三个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let str_innerHtml = elm_span .innerHTML;
        let inputStrnig = str_innerHtml.trim().replace('<br>',"");

        return map_solution['Array Jumping Game'](inputStrnig);
    },
    'Merge Overlapping Intervals':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取第三个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let str_innerHtml = elm_span .innerHTML;
        let inputStrnig = str_innerHtml.trim().replace('<br>',"");
        return map_solution['Merge Overlapping Intervals'](inputStrnig);
    },
    'Algorithmic Stock Trader II':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取第三个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[2];
        let str_innerHtml = elm_span .innerHTML;
        let inputStrnig = str_innerHtml.trim().replace('<br>',"");
        return map_solution['Algorithmic Stock Trader II'](inputStrnig);
    },
    'Encryption I: Caesar Cipher':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取倒数第四个span
        let span_list = elm_p.querySelectorAll('span');
        let span_4 = span_list[3];
        // 取第四span
        let str_input = span_4.innerHTML;
        str_input.trim();
        str_input = str_input.replace('<br>','');
        str_input = str_input.replace(/&nbsp;/g,'');

        return map_solution['Encryption I: Caesar Cipher'](str_input);
    },
    'Encryption II: Vigenère Cipher':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 取倒数第四个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[span_list.length-4];
        let str_input = elm_span.innerHTML;
        str_input = str_input.trim();
        str_input = str_input.replace('<br>','');
        str_input = str_input.replace(/&nbsp;/g,'');

        return map_solution['Encryption II: Vigenère Cipher'](str_input);
    },
    'Spiralize Matrix':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 从第三个span到"here"
        let span_list = elm_p.querySelectorAll('span');
        let input_str = "";
        for(let i=2;i<span_list.length;++i)
        {
            let str_html = span_list[i].innerHTML;
            if(-1!=str_html.indexOf( 'Here'))break;
            str_html=str_html.replace(/&nbsp;/g,'');
            str_html=str_html.replace(/<br>/g,'');
            input_str+=str_html;
            if(3<str_html.length)input_str+=',';
        }
        return map_solution['Spiralize Matrix'](input_str);
    },
    'HammingCodes: Integer to Encoded Binary':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第二个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[1];
        let str_input = elm_span.innerHTML.trim().replace('<br>','');
        
        return map_solution['HammingCodes: Integer to Encoded Binary'](str_input);

    },
    'HammingCodes: Encoded Binary to Integer':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第二个span
        let span_list = elm_p.querySelectorAll('span');
        let elm_span = span_list[1];
        let str_input = elm_span.innerHTML.trim().replace('<br>','');
        return map_solution['HammingCodes: Encoded Binary to Integer'](str_input);
    },
    'Unique Paths in a Grid I':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第三span
        let span_list = elm_p.querySelectorAll('span');
        let str_input = span_list[4].innerHTML.replace('<br>','').replace(/&nbsp;/g,'').trim();
        return map_solution['Unique Paths in a Grid I'](str_input);
    },
    'Unique Paths in a Grid II':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第三span 到 第13span
        let bool_first_time = true;
        let span_list = elm_p.querySelectorAll('span');
        let number_of_comma = 0;
        let str_input = '';
        for(let i=2;i< span_list.length&&i<13;++i)
        {
            let elm_span = span_list[i];
            let str = elm_span.innerHTML;
            str=str.trim().replace('<br>','').replace(/&nbsp;/g,'');
            if(bool_first_time)
            {
                bool_first_time = false;
                // 一行中有多少逗号
                for(let j=0;j<str.length;++j)
                {
                    let item = str[j];
                    if(','==item)
                    {
                        ++number_of_comma;
                    }
                }
            }
            str_input+=str;
        }
        ns.print("#解析:多少逗号:",number_of_comma);

        return map_solution['Unique Paths in a Grid II'](str_input,number_of_comma);

    },
    'Find All Valid Math Expressions':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第9个span
        let bool_first_time = true;
        let span_list = elm_p.querySelectorAll('span');
        let span = span_list[8];
        let str_input = span.innerHTML.trim().replace(/&nbsp;/g,'').replace('<br>','');        

        return map_solution['Find All Valid Math Expressions'](str_input);
    },
    'Generate IP Addresses':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第3个span
        let span_list = elm_p.querySelectorAll('span');
        let span = span_list[2];
        let str_input = span.innerHTML.trim().replace(/&nbsp;/g,'').replace('<br>','');

        return map_solution['Generate IP Addresses'](str_input);
    },
    'Compression III: LZ Compression':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第9个span
        let span_list = elm_p.querySelectorAll('span');
        let span = span_list[8];
        let str_input = span.innerHTML.trim().replace(/&nbsp;/g,'').replace('<br>','');
        
        return map_solution['Compression III: LZ Compression'](str_input);
    },
    'Compression II: LZ Decompression':(elm)=>{
        let ns = GNS;
        let elm_list = elm.querySelectorAll('p');
        // 取第二个p 第一个p 必然是统一的声明
        let elm_p = elm_list[1];
        // 第9个span
        let span_list = elm_p.querySelectorAll('span');
        let span = span_list[8];
        let str_input = span.innerHTML.trim().replace(/&nbsp;/g,'').replace('<br>','');

        return map_solution['Compression II: LZ Decompression'](str_input);
    },

    

}















export async function main(ns) {
    ns.tail("execute-a-command.js");
    // home;connect home;connect sigma-cosmetics;connect CSEC;run contract-267894-Netburners.cct;
    // MuiInput-root

    GNS = ns;

    // 用于模拟点击:
    var eventClick = new MouseEvent('click', {
        'view': window,
        'bubbles': true,
        'cancelable': true
    });

    // 优先检测合约子窗口:
    {
        let elements_list0 = document.getElementsByClassName("css-fq5ump-paper");//jss20
        if(elements_list0.length)
        {
            // let elem_jss20 = document.getElementsByClassName("jss20")
            
            // if (typeof elem_jss20 === "undefined") {
            // }
            // else

            let elem_jss20 = elements_list0[0];

            {
                // ns.print("typeof elem1:",typeof elem_jss20);
                // ns.print("elem1.length:",elem_jss20.length);
                //if(elem_jss20.length && elem_jss20)

                // .MuiBox-root:
                let elm_muibox_root = elem_jss20.querySelector("div.MuiBox-root");

                {
                    let elem2 = elem_jss20.querySelectorAll('h4')[0];
                    let title = elem2.innerHTML;
                    ns.print("合约:",title);

                    if("Subarray with Maximum Sum" == title)
                    {
                        // 找到题目中的数据:
                        let list_of_element_1 = elem_jss20.querySelectorAll("p.MuiTypography-root");
                        //ns.print("找到的标签:",list_of_element_1.length,"\t",".MuiTypography-root");
                        // 取第二个<p>
                        let p2 = list_of_element_1[1];
                        let span_list = p2.querySelectorAll('span');
                        let span2 = span_list[1];
                        let str_input_data1=span2.innerHTML;
                        let index_br = str_input_data1.indexOf('<br>');
                        str_input_data1=str_input_data1.substring(0,index_br);
                        ns.print("题目的数据:",span2.innerHTML);
                        ns.print("题目的数据:",str_input_data1);
                        ns.print("计算结果:");
                        let result = ""+map_solution[title](str_input_data1);
                        ns.print(result);

                        // // BEGIN 把答案填入表单:
                        // // 找MuiInputBase-root
                        // let elm_from = elem_jss20.querySelectorAll('.MuiInputBase-root')[0];
                        // elm_from.dispatchEvent(eventClick);
                        // // 找input:
                        // let elm_input = elm_from.querySelector('input');
                        // elm_input.value = result;
                        // // END 把答案填入表单
                    }
                    else
                    {
                        // 表单放进去让它自己去解:
                        let result = map_analyze_contract[title](elm_muibox_root);
                        ns.print("运算结果:",result);
                        // // BEGIN 把答案填入表单:// 这么做是危险的
                        // // 找MuiInputBase-root
                        // let elm_from = elem_jss20.querySelectorAll('.MuiInputBase-root')[0];
                        // elm_from.dispatchEvent(eventClick);
                        // // 找input:
                        // let elm_input = elm_from.querySelector('input');
                        // elm_input.value = result;
                        // // END 把答案填入表单

                        // // BEGIN 模拟点击 手动点才行
                        // let list_button = elm_muibox_root.querySelectorAll('button');
                        // ns.print("按钮数:",list_button.length);
                        // if(1==list_button.length)
                        // {
                        //     let elem_button = list_button[0];
                        //     elem_button.dispatchEvent(eventClick);
                        // }
                        // // END 模拟点击
                    }

                    return;
                }
            }
        }
        else{
            ns.print("#1215 没有找到对应的表单");
            let total = 0;
            for(let i in map_analyze_contract)
            {
                ++total;
            }
            ns.print("目前有多少个算法:",total);
        }

    }
    // END 检测合约子窗口
    

    // element.dispatchEvent(event);

    // let element0 = document.getElementsByClassName("MuiInput-root")[0];
    // element0.dispatchEvent(eventClick);
    // await ns.sleep(100);
    // //ns.print(a.innerHTML);
    // let element1 = element0.querySelectorAll('input')[0];
    // ns.print("原有的值:",element1.value)
    // element1.value = 'home;connect home;connect sigma-cosmetics;connect CSEC;run contract-267894-Netburners.cct;';
    // element1.setAttribute('value','home;connect home;connect sigma-cosmetics;connect CSEC;run contract-267894-Netburners.cct;');
    // ns.print("#12 :",element1.innerHTML);
    // ns.print("element1.value:",element1.value)

    // //这一段无效:
    // // 创建一个键盘事件
    // var keyboardEvent = new KeyboardEvent('keydown', {
    //     bubbles: true,
    //     cancelable: true,
    //     keyCode: 13 // 回车键的键码
    // });
    // await ns.sleep(100);
    // element1.dispatchEvent(keyboardEvent);

    //    //这一段无效:
    // // 创建一个键盘事件
    // var keyboardEvent = new KeyboardEvent('keydown', {
    //     bubbles: true,
    //     cancelable: true,
    //     keyCode: 32 
    // });
    // await ns.sleep(100);
    // element1.dispatchEvent(keyboardEvent);

}